package dao;

import java.util.List;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import entidade.Fatura;

@SuppressWarnings("unchecked")
public class FaturaDAO  {
	
	public void salve(Fatura fatura) {
		Session sessao = null;
		try {
			sessao = Conexao.getConexao();
			Transaction transacao = sessao.beginTransaction();
			sessao.save(fatura);
			transacao.commit();
		} finally {
			sessao.close();
		}
	}

	public void update(Fatura fatura) {
		Session sessao = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			Transaction transacao = sessao.beginTransaction();
			sessao.update(fatura);
			transacao.commit();
		} finally {
			sessao.close();
		}
	}

	public void delete(Fatura fatura) {
		Session sessao = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			Transaction transacao = sessao.beginTransaction();
			sessao.delete(fatura);
			transacao.commit();
		} finally {
			sessao.close();
		}
	}

	public List<Fatura> list() {
		Session sessao = null;
		Transaction transacao = null;
		Query consulta = null;
		List<Fatura> lista = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			transacao = sessao.beginTransaction();
			consulta = sessao.createQuery("from Fatura");
			lista = consulta.list();
			transacao.commit();
			return lista;
		} finally {
			sessao.close();
		}
	}

	public Fatura find(Integer codigo) {
		Session sessao = null;
		Transaction transacao = null;
		Query consulta = null;
		Fatura fatura = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			transacao = sessao.beginTransaction();
			consulta = sessao
					.createQuery("from Cartao where codigo = :parametro");
			consulta.setInteger("parametro", codigo);
			fatura = (Fatura) consulta.uniqueResult();
			transacao.commit();
			return fatura;
		} finally {
			sessao.close();
		}
	}	

}
